﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：工资录入
 */
var companyId = request('companyId');
var ztId = request('ztId');
var keyValue = request('keyValue');

var acceptClick;
var columns = [];
var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {
            //基本信息
            $.ajax({
                url: config.base_server() + 'users/' + keyValue + '?access_token=' + config.getToken().access_token,
                data: {},
                type: 'GET',
                success: function (data) {
                    $('#userCode').val(data.userCode);
                    $('#realName').val(data.realName);
                    $('#category').val(data.category);
                    $('#position').val(data.position);
                    $('#levels').val(data.levels);
                    $('#nature').val(data.nature);
                    $('#job').val(data.job);
                    $('#technicalLevel').val(data.technicalLevel);
                }
            });

            $.lrSetForm(config.base_server() + 'gzitems/dropdownlist?companyId=' + companyId + '&ztId=' + ztId, function (data) {
                var $warp = $('#ext_info');
                $warp.empty();
                columns = [];
                for (var i = 0, l = data.length; i < l; i++) {
                    var exists = $("#" + data[i].id).length > 0;
                    if (exists == false) {
                        var _html = "";
                        _html += ' <div class="col-xs-6 lr-form-item">';
                        _html += '     <div class="lr-form-item-title">' + data[i].text + '</div>';
                        _html += '     <input id="col_' + data[i].id + '" type="number"  class="form-control" />';
                        _html += ' </div>';

                        $warp.append($(_html));
                        columns.push(data[i].id);
                    }
                }

                $.ajax({
                    url: config.base_server() + 'gzrecordsalarys/findByUserid?companyId=' + companyId + '&ztId=' + ztId + '&userid=' + keyValue + '&access_token=' + config.getToken().access_token,
                    data: {},
                    type: 'GET',
                    success: function (res) {
                        jQuery.each(res.data[0], function (key, val) {
                            if (key.indexOf("col_") >= 0) {
                                $("#" + key).val(val);
                            }
                        });
                    }
                });

                $("input").keydown(function () {
                    if (13 == event.keyCode) {
                        var form = $("body");
                        var inputEles = form.find("input[disabled!='disabled']");
                        console.log("inputEles -> " + inputEles.length);  //5
                        for (var i = 0; i < inputEles.length; i++) {
                            if ($(inputEles[i]).is($(this))) {//判断两个jQuery对象是否相等用is
                                if (i == inputEles.length - 1) {
                                    $(inputEles[0]).focus();
                                } else {
                                    $(inputEles[i + 1]).focus();
                                }
                            }
                        }
                    }
                });
            });
        },
        initData: function () {

        }
    };
    // 保存数据
    acceptClick = function (callBack) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var postData = $('#ext_info').lrGetFormData(keyValue);
        postData.companyId = companyId;
        postData.ztId = ztId;
        postData.userid = keyValue;
        postData.usercode = $("userCode").val();
        postData.username = $("#realName").val();

        var records = [];

        for (var i = 0; i < columns.length; i++) {
            var item = {};
            item.itemId = columns[i];
            item.value = $("#col_" + columns[i]).val();
            records.push(item);
        }
        postData.records = records;

        $.lrSaveForm(config.base_server() + 'gzrecordsalarys/saveOrUpdate1?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };
    page.init();
}
